package com.ipark.basic.application.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.alibaba.fastjson.JSON;
import com.ipark.basic.application.dao.UserMapper;
import com.ipark.basic.application.pojo.Kl;
import com.ipark.basic.application.pojo.User;

import reactor.core.publisher.Mono;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@SuppressWarnings("javadoc")
@RestController
public class HelloController {
	Logger logger = LogManager.getLogger(getClass());

	
	@Autowired
	private UserMapper userMapper;


	@RequestMapping("/")
	public Mono<String> index() {
		//logger.info(userMapper.findUser().getBusuuid());
		User user=userMapper.findUser();
		return Mono.just(JSON.toJSONString(user));
	}
	
	

	@RequestMapping("/user")
	public Mono<User> user() {
		//logger.info(userMapper.findUser().getBusuuid());
		User user=userMapper.findUser();
		return Mono.just(user);
	}
	
	
	
	
	 @Autowired
	    private JdbcTemplate jdbcTemplate;

	
	@RequestMapping("/kl")
	public Mono<String> FindKL() {
		//logger.info(userMapper.findUser().getBusuuid());
		String result=jdbcTemplate.queryForObject("select pfrBusName from kl order by pfrUploadTime limit 1",String.class);
		return Mono.just(result);
	}
	
	
	@RequestMapping("/kls")
	    public Mono<List<Kl>> findKLS() {
	        String sql = "select pfrBusName,pfrBeforeLat,pfrBeforeLong from kl order by pfrUploadTime limit 100 ";
	        List<Kl> kls = jdbcTemplate.query(sql, (rs, rowNum)->{
	            return this.initKls(rs);
	        });
	       
	        return Mono.just(kls);
	    }
	
	  private Kl initKls(ResultSet rs) throws SQLException {
	        if(rs==null){
	            return null;
	        }
	        Kl s = new Kl();
	        s.setPfrBeforeLat(String.valueOf(rs.getString("pfrBeforeLat")));
	        s.setPfrBeforeLong(String.valueOf(rs.getString("pfrBeforeLong")));
	        s.setPfrBusName(String.valueOf(rs.getString("pfrBusName")));
	        return s;
	    }

}